package com.atguigu.tingshu.common.delay;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DelayMsgService {

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 发送延迟消息
     */
    public void sendDelayMessage(String queueName,String msg,Long delay){
      try{
          //阻塞对象
          RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque(queueName);
          //延迟对象
          RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
//          delayedQueue.offer(msg,delay, TimeUnit.MINUTES);
          delayedQueue.offer(msg,delay, TimeUnit.SECONDS);
          log.info("延迟关单消息发送成功，队列：{}，发送消息：{}",queueName,msg);
      }catch (Exception e){
          log.error("延迟关单消息发送异常，队列：{}，发送消息：{}",queueName,msg);
          throw new RuntimeException(e);
      }
    }
}
